-- defeat_airship
-- Created by panyl Nov/04/2016
-- 成就：打败飞艇

-- 内部函数声明
local whenDefeatAirship;

-- 关注怪物死亡的事件
EventMgr.register("achievements/defeat_airship", event.DEFEAT_AIRSHIP, function(args)
    whenDefeatAirship(args);
end);

-- 击败飞艇
whenDefeatAirship = function(args)
    local class_id = args.class_id;

    -- 玩家不在迷宫中
    if not DungeonM.isInDungeon() then
        return;
    end

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("defeat_airship");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end

        -- 判断是否是需求怪物
        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) ~= "table" or ruleArg["class_id"] ~= class_id then
            return false;
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};
